package com.artezio.testapp.domain;

import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

@Entity
@Table(name = "city")
public class City {
	
	public City(){
		super();
	}
	
	public City(Integer id, String name, Long population){
		this.id = id;
		this.name = name;
		this.population = population;
	}

	@Id
	@Column(name = "id")
	@GeneratedValue
	private Integer id;
	
	@Column(name = "name")
	private String name;

	@Column(name = "population")
	private Long population;
	
	@OneToMany(fetch = FetchType.LAZY, mappedBy="city", cascade = CascadeType.ALL)
	private Set<Person> persons;

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Long getPopulation() {
		return population;
	}

	public void setPopulation(Long population) {
		this.population = population;
	}

	public Set<Person> getPersons() {
		return persons;
	}

	public void setPersons(Set<Person> persons) {
		this.persons = persons;
	}
	
	@Transient
	public String getPersonsAsList() {
		
		if (persons==null) return null;
		
		StringBuilder stringBuilder = new StringBuilder();
		
		for (Person person: persons){
			if(stringBuilder.length()>0){
				stringBuilder.append(", ");
			}
		
			stringBuilder.append(person.getName());
		}
		
		return stringBuilder.toString();
	}

}
